package com.ease.component.controls {

   import com.ease.component.events.TabNavigationEvent;

   import flash.events.Event;

   import mx.collections.IList;
   import mx.controls.Button;
   import mx.controls.TabBar;
   import mx.core.ClassFactory;
   import mx.core.IFlexDisplayObject;
   import mx.core.mx_internal;

   use namespace mx_internal;

   public class ClosableTabBar extends TabBar {

      private var _tabMaxWidth:Number;

      private var tabMaxWidthChanged:Boolean = false;

      public function get tabMaxWidth():Number {
         return _tabMaxWidth;
      }

      public function set tabMaxWidth(value:Number):void {
         if (_tabMaxWidth != Math.floor(value)) {
            _tabMaxWidth = Math.floor(value);
            tabMaxWidthChanged = true;
            invalidateProperties();
         }
      }

      private var _showCloseIconFunction:Function = null;
      [Bindable("showCloseIconFunctionChanged")]
      public function get showCloseIconFunction():Function {
         return _showCloseIconFunction;
      }

      public function set showCloseIconFunction(value:Function):void {
         _showCloseIconFunction = value;
         dispatchEvent(new Event("showCloseIconFunctionChanged"));
      }

      override protected function commitProperties():void {
         super.commitProperties();

         if (tabMaxWidthChanged == true) {
            for (var i:int = 0; i < numChildren; i++) {
               var child:Button = getChildAt(i) as Button;
               if (child) {
                  child.maxWidth = _tabMaxWidth;
               }
            }

            tabMaxWidthChanged = false;
         }
      }

      /**
       * Constructor
       */
      public function ClosableTabBar() {
        super();
        navItemFactory = new ClassFactory(TabButton);
      }

      override protected function createChildren():void {
         super.createChildren();

         addEventListener(TabNavigationEvent.CLOSE_TAB, updateTabBar);
      }

      /**
       * @private
       * Override createNavItem so that we always create a TabButton instead
       * of a tab.
       *
       * @param label
       * @param icon
       *
       * @return
       */
      override protected function createNavItem(label:String, icon:Class = null):IFlexDisplayObject {
         var tab:TabButton = super.createNavItem(label,icon) as TabButton;
         tab.index = getChildIndex(tab);
         var event:TabNavigationEvent = new TabNavigationEvent(TabNavigationEvent.CREATE_TAB, tab.index);
         event.tabButton = tab;
         dispatchEvent(event);
         return tab;
      }

      private function updateTabBar(event:TabNavigationEvent):void {
         // update tab button index.
         for (var itr:int = event.tabIndex + 1; itr < numChildren; itr++) {
            var tab:TabButton = getChildAt(itr) as TabButton;
            tab.index--;
         }

         // update data provider.
         (dataProvider as IList).removeItemAt(event.tabIndex);
      }
   }
}
